package com.bingyuan.aipassproject.view.widget;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bingyuan.aipassproject.R;
import com.bingyuan.aipassproject.utils.ShareUtil;
import com.bingyuan.aipassproject.utils.Utils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;

public abstract class MinePopwindow {

    private PopupWindow popupWindow;
    private Activity context;
    private String face;

    /**
     * 构造函数
     *
     * @param context
     */
    public MinePopwindow(Activity context,String face) {
        this.context = context;
        this.face = face;
    }

    /**
     * 显示警示框
     */
    public void show() {
        View popView = View.inflate(context, R.layout.pop_personal, null);
        popupWindow = new PopupWindow(context);
        popupWindow.setAnimationStyle(R.style.popWindow_animation);//设置动画
        popupWindow.setOutsideTouchable(false);
        popupWindow.setBackgroundDrawable(null);
        popupWindow.setFocusable(true);
        popupWindow.setContentView(popView);
        popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        ImageView ivLogin = popView.findViewById(R.id.ivChangeUser);
        TextView tvPhone = popView.findViewById(R.id.tvPhone);
        ImageView ivPopHeader = popView.findViewById(R.id.ivPopHeader);
        ImageView ivLoginOut = popView.findViewById(R.id.ivLoginOut);
        if (Utils.isLogin()){
            ivLogin.setImageResource(R.drawable.icon_change_login);
            tvPhone.setText(ShareUtil.getInstance().get(ShareUtil.PHONE));
            Glide.with(context).load(face)
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(ivPopHeader);
//            ivPopHeader.setImageResource(R.drawable.icon_csc);
            ivLoginOut.setVisibility(View.VISIBLE);
        }else {
            ivLogin.setImageResource(R.drawable.icon_go_login);
            tvPhone.setText("未登录");
            ivPopHeader.setImageResource(R.drawable.icon_pop_header);
            ivLoginOut.setVisibility(View.GONE);
        }
        //切换用户
        popView.findViewById(R.id.ivChangeUser).setOnClickListener(v -> changeUser());

        //关闭
        popView.findViewById(R.id.ivClose).setOnClickListener(v -> popupWindow.dismiss());

        //账变明细
        popView.findViewById(R.id.rlDetailed).setOnClickListener(v -> getDetail());

        //游戏记录
        popView.findViewById(R.id.rlGameRecord).setOnClickListener(v -> gameRecord());

        //兑换金币
        popView.findViewById(R.id.rlExchangeCoin).setOnClickListener(v -> exchangeCoin());

        //联系客服
        popView.findViewById(R.id.rlCallCustomer).setOnClickListener(v -> callCustomer());

        //退出登录
        popView.findViewById(R.id.ivLoginOut).setOnClickListener(v -> loginOut());

        popupWindow.setOnDismissListener(() -> onDisMiss());

    }

    /**
     * 取消键按下后执行
     */
    public abstract void loginOut();

    /**
     * 切换用户
     */
    public abstract void changeUser();

    /**
     * 账变明细
     */
    public abstract void getDetail();

    /**
     * 游戏记录
     */
    public abstract void gameRecord();

    /**
     * 兑换金币
     */
    public abstract void exchangeCoin();

    /**
     * 联系客服
     */
    public abstract void callCustomer();

    /**
     * 消失
     */
    public abstract void onDisMiss();

    public void disMiss(){
        popupWindow.dismiss();
    }
}
